/* Copyright 2011 Gilberto Pacheco Gallegos.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License. */
package net.ramptors.dominiobd.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.InvocationException;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.web.bindery.event.shared.UmbrellaException;

/**
 * @author Gilberto Pacheco Gallegos
 */
public class DominioBdEntryPoint implements EntryPoint {
  @Override
  public void onModuleLoad() {
    GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
      @Override
      public void onUncaughtException(final Throwable t) {
        procesa(t);
      }
    });
    final DominioBd dominioBd = new DominioBd();
    dominioBd.actualiza();
    RootLayoutPanel.get().add(dominioBd);
    Window.enableScrolling(false);
  }
  private void procesa(final Throwable t) {
    try {
      throw t;
    } catch (final InvocationException ex) {
      Window.alert("Hay un fallo en el servidor.");
    } catch (final IncompatibleRemoteServiceException ex) {
      Window.alert(
          "El servidor no es compatible con la aplicación. Recargando la página...");
      Window.Location.reload();
    } catch (final ExcepcionDeAplicacion ex) {
      Window.alert(ex.getLocalizedMessage());
    } catch (final UmbrellaException ex) {
      GWT.log("Umbrella Exception.", ex);
      for (final Throwable throwable : ex.getCauses()) {
        procesa(throwable);
      }
    } catch (final Throwable ex) {
      GWT.log("Error no atrapado.", ex);
      Window.alert(ex.getLocalizedMessage());
    }
  }
}